package com.ys.erp.stage_mall_service.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graphbuilder.org.apache.harmony.awt.gl.Crossing;
import com.ys.erp.common.enums.RedisKey;
import com.ys.erp.common.util.DateUtil;
import com.ys.erp.entity.data.bean.MallHomeActivity;
import com.ys.erp.entity.data.bean.MallHomeActivityCoupons;
import com.ys.erp.entity.service.IMallHomeActivityCouponsService;
import com.ys.erp.entity.service.IMallHomeActivityService;
import com.ys.erp.stage_mall_service.bean.vo.HomeActivityVO;
import com.ys.erp.stage_mall_service.service.HomeMallActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author minamiyama
 * @date 2025/6/6 14:18
 */
@Service
@Log4j
@RequiredArgsConstructor
public class ActivityTask {
    private final IMallHomeActivityService iMallHomeActivityService;
    private final RedissonClient redisson;
    private final IMallHomeActivityCouponsService iMallHomeActivityCouponsService;
    private final RedisTemplate<String, Object> redisTemplate;

    @Scheduled(cron = "0 0 0 * * ?")
    public void activityStatusTask() {
        RLock lock = redisson.getLock(RedisKey.Enums.ACTIVITY_COUPONS_REFRESH.getKey());
        boolean b = lock.tryLock();
        if (!b) {
            return;
        }
        try {
            List<MallHomeActivity> activities = iMallHomeActivityService.list();
            for (MallHomeActivity activity : activities) {
                activity.setActivityType(activity.getActivityStatus(DateUtil.localDateTimeFormat(activity.getStartTime()), DateUtil.localDateTimeFormat(activity.getEndTime())));
                if (!activity.getActivityType().equals(MallHomeActivity.Enums.ONGOING.toString())) {
                    continue;
                }
                LambdaQueryWrapper<MallHomeActivityCoupons> couponQw = new LambdaQueryWrapper<>();
                couponQw.eq(MallHomeActivityCoupons::getActivityNo, activity.getNo());
                List<MallHomeActivityCoupons> coupons = iMallHomeActivityCouponsService.list(couponQw);
                for (MallHomeActivityCoupons coupon : coupons) {
                    redisTemplate.opsForValue().set(RedisKey.Enums.ACTIVITY_COUPONS.getKey() + activity.getNo()
                            + ":" + coupon.getCouponsId(), coupon.getSendNum(), DateUtil.getMinutesUntil(DateUtil.localDateTimeFormat(activity.getEndTime())), TimeUnit.MINUTES);
                }

            }
            iMallHomeActivityService.updateBatchById(activities);

        } finally {
            lock.unlock();
        }
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void activityCoupon() {
        RLock lock = redisson.getLock(RedisKey.Enums.ACTIVITY_COUPONS_REFRESH.getKey());
        boolean b = lock.tryLock();
        if (!b) {
            return;
        }
        try {
            LambdaQueryWrapper<MallHomeActivityCoupons> couponsQw = new LambdaQueryWrapper<>();
            couponsQw.eq(MallHomeActivityCoupons::getSendMethod, MallHomeActivityCoupons.Enums.DAILY.toString());
            List<MallHomeActivityCoupons> coupons = iMallHomeActivityCouponsService.list(couponsQw);
            for (MallHomeActivityCoupons coupon : coupons) {
                LambdaQueryWrapper<MallHomeActivity> activityQw = new LambdaQueryWrapper<>();
                activityQw.eq(MallHomeActivity::getNo, coupon.getActivityNo());
                activityQw.eq(MallHomeActivity::getActivityStatus, MallHomeActivity.Enums.ONGOING.toString());
                MallHomeActivity activity = iMallHomeActivityService.getOne(activityQw);
                if (Objects.isNull(activity)) {
                    continue;
                }
                redisTemplate.opsForValue().set(RedisKey.Enums.ACTIVITY_COUPONS.getKey() + activity.getNo() + ":" + coupon.getCouponsId(), coupon.getSendNum(), DateUtil.getMinutesUntil(activity.getEndTime().toString()), TimeUnit.MINUTES);
            }
        } finally {
            lock.unlock();
        }
    }


}
